package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.R;
import com.example.demo.dto.BalanceDto;
import com.example.demo.entity.Balance;
import com.example.demo.entity.BalanceArea;
import com.example.demo.mapper.BalanceMapper;
import com.example.demo.service.BalanceAreaService;
import com.example.demo.service.BalanceFoodRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/balance")
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
public class BalanceController {
    /**
     *  新增方案的话  是在方案联系表里创建一个varchar存入一个数组 ,
     *  然后就可以一个balanceID对应一堆维生素或者微量元素和食物了
     *  同理新增食物的时候也是这样 ,这样才不能使trace和vitamin里的数据重复出现 , 也可以复用
     */

    /**
     * 食物联系表
     */
    @Autowired
    private BalanceFoodRelationService balanceFoodRelationService;

    @Autowired
    private BalanceMapper balanceMapper;

    @Autowired
    private BalanceAreaService balanceAreaService;


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        log.info("id{}", id);
        balanceFoodRelationService.deleteBalanceFoodRelation(id);
        return R.success("删除成功");
    }

    /**
     * 查到所有
     *
     * @return
     */
    @GetMapping("/all")
    public R<List<Balance>> getAll() {
        log.info("all{}");
        List<Balance> balances = balanceMapper.selectList(null);
        return R.success(balances);
    }

    /**
     * 分页查询方案
     *
     * @param page
     * @param pageSize
     * @param planName
     * @return
     */
    @GetMapping("/page")
    public R<Page> Page(int page, int pageSize, String planName) {
        log.info("page = {},pageSize = {},planName = {}", page, pageSize, planName);

        Page<Balance> allBalance = balanceFoodRelationService.getAllBalance(page, pageSize, planName);

        return R.success(allBalance);
    }

    /**
     * 根据ID查询方案
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<BalanceDto> getById(@PathVariable Long id) {
        log.info("根据id查询方案信息...");

        BalanceDto byIdBalance = balanceFoodRelationService.getByIdBalance(id);

        return R.success(byIdBalance);
    }


    /**
     * 增加方案
     *
     * @param balanceDto
     * @return
     */
    @PostMapping("/add")
    public R<String> save(@RequestBody BalanceDto balanceDto) {
        log.info("新增方案，方案信息：{}", balanceDto.toString());

        balanceFoodRelationService.saveBalance(balanceDto);

        return R.success("新增方案成功");
    }

    /**
     * 修改方案
     *
     * @param balanceDto
     * @return
     */
    @PatchMapping
    @Transactional
    public R<String> add(@RequestBody BalanceDto balanceDto) {
        log.info("修改方案，方案信息：{}", balanceDto.toString());

        balanceFoodRelationService.updateBalance(balanceDto);

        return R.success("修改成功");
    }


    /**
     * 查询对应地区的方案
     * @param province
     * @return
     */
    @GetMapping("/province")
    public R<List<Balance>> getBalanceArea(String province){

        List<Balance> allBalanceArea = balanceAreaService.getAllBalanceArea(province);

        return R.success(allBalanceArea);
    }

    /**
     * 修改对应地区的方案
     * @param balanceArea
     * @return
     */
    @PatchMapping("/area")
    @Transactional
    public R<String> updateBalanceArea(BalanceArea balanceArea){
        balanceAreaService.updateById(balanceArea);
        return R.success("修改成功");
    }

}
